package com.nc.delivery_project.server.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.nc.delivery_project.server.dao.AbstractDao;
import com.nc.delivery_project.server.dao.CityDao;
import com.nc.delivery_project.server.dao.DepartmentDao;
import com.nc.delivery_project.server.db_util.ConnectionManager;
import com.nc.delivery_project.server.domain.City;
import com.nc.delivery_project.server.domain.Department;
import com.nc.delivery_project.server.domain.Role;
import com.nc.delivery_project.server.domain.StateProvince;
import com.nc.delivery_project.server.domain.User;
import com.nc.delivery_project.server.util.CRUD;

public class CityService extends BaseService<City> {
	
	

	public CityService() {
		super(new CityDao());
	}
	
	

	public CityService(AbstractDao<City> cityDao) {
		super( cityDao);
	}



	public CityService(Connection connection, boolean isAutoCloseConnection) {
        super(new CityDao(), connection, isAutoCloseConnection);
	}




	@Override
	public List<City> read() {
		 List<City> cities = null;
	        
	        try {
	        	 Connection conn = getConnection();
	             dao.setConnection(conn);
	             cities = dao.read();
	            
	            StateProvinceService statePrService = new StateProvinceService(conn, false);
	            
	            for (City city : cities) {
	                 city.setStatePr(statePrService.read(city.getStatePr().getStateId()));
	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                closeConnection();
	            } catch (SQLException e) {
	                e.printStackTrace();
	            }
	        }

	    return cities;
	}

	@Override
	public City read(int id) {
		City city = null;
	        
	        try {
	            Connection conn = getConnection();
	            dao.setConnection(conn);
	            city = dao.read(id);
	            
	            if (city != null) {
	            	StateProvince cityStatePr = new StateProvinceService(conn, false).read(city.getStatePr().getStateId());
	                if (cityStatePr != null) {
	                	city.setStatePr(cityStatePr);
	                    city.getStatePr().setStateTitle(cityStatePr.getStateTitle());
	                    city.getStatePr().setCountry(cityStatePr.getCountry());
	                }
	            }
	        } catch (SQLException e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                closeConnection();
	            } catch (SQLException e) {
	                e.printStackTrace();
	            }
	        }

	   return city;
	}


    public List<Department> loadCityDepartments(int cityId) throws SQLException {
        List<Department> departments = new DepartmentService().loadDeptByCityId(cityId);
        return departments;
    }

	
	public List<City> loadCityByStatePrId(int statePrId) throws SQLException {
		List<City> cities = null;
        
        try {
        	 Connection conn = getConnection();
             dao.setConnection(conn);
             cities =((CityDao)dao).readByStateProvinceId(statePrId);
            
            StateProvinceService statePrService = new StateProvinceService(conn, false);
            
            for (City city : cities) {
                 city.setStatePr(statePrService.read(city.getStatePr().getStateId()));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cities;

	}

}
